class MyBlockingQueue {
    private int left;
    private int term;
    private int[] data = null;
    private int size;
    private Object lock=new Object();
    public MyBlockingQueue(int capition) {
        this.data = new int[capition];
    }

    public void take(int key) throws InterruptedException {
        synchronized (lock) {
            while (data.length == size) {
            lock.wait();
            }
            data[term]=key;
            term++;
            if(term>=size){
                term=0;
            }
            size++;
            lock.notify();
        }
    }

    public int put() throws InterruptedException {
        int ret=0;
        synchronized (lock){
            while (size==0){
                lock.wait();
            }
            ret=data[left];
            left++;
            if(left>data.length){
                left=0;
            }
            size--;
            lock.notify();
        }
        return ret;
    }
}


public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        MyBlockingQueue myBlockingQueue=new MyBlockingQueue(3);
        myBlockingQueue.take(1);
        myBlockingQueue.take(2);
        myBlockingQueue.take(3);
        System.out.println("成功添加3");
        myBlockingQueue.take(4);
        System.out.println("成功添加4");
        myBlockingQueue.put();
        System.out.println("成功释放");
    }
}
